package com.cqie.wanling.controller;


import com.burial.common.log.MyLog;
import com.cqgc.entity.User;
import com.cqgc.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.cqie.wanling.utils.RedisUtil;
import com.cqie.wanling.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @ Program       :  com.cqie.wanling.filter.JWTFilter
 * @ Description   :
 * @ Author        :  cc
 * @ CreateDate    :  2024-4-17
 */
@Controller
@Api(tags = "登录接口")
public class LoginController {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("用户登录")
    @MyLog(type = "用户登录", msg = "用户登录操作")  //这里使用的自定义注解
    public String login(String account, String password, HttpServletResponse response) throws JsonProcessingException {
        System.err.println(2345);
        User user=userService.checkLogin(account,password);
        Long currentTimeMillis = System.currentTimeMillis();
        if(user!=null) {
            if(user.getState()!=-1) {
                int role = userService.getRoleByAccount(user.getAccount());
                String userRole = "";
                if (role == 1) {
                    userRole = "管理员";
                }
                if (role == 2) {
                    userRole = "供应商";
                }
                if (role == 3) {
                    userRole = "采购方";
                }
                String token = TokenUtil.sign(account, currentTimeMillis, userRole);
//                RedisUtil.set(account, token, TokenUtil.REFRESH_EXPIRE_TIME);
                //将account作为key,token作为value，并设置过期时间5分钟，存入Redis缓存
                System.err.println("token = " + token);
                RedisUtil.set(account, token, 60*3600);
                response.setHeader("Authorization", token);
                response.setHeader("Access-Control-Expose-Headers", "Authorization");
                HashMap<String, Object> hs = new HashMap<>();
                hs.put("login", "登录成功");
                ObjectMapper objectMapper = new ObjectMapper();

                return objectMapper.writeValueAsString(hs);
            }else{
                System.err.println(9999);
                HashMap<String, Object> hs = new HashMap<>();
                hs.put("login", "用户已冻结");
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.writeValueAsString(hs);
            }
        }else{
            System.err.println(6666);
            HashMap<String, Object> hs = new HashMap<>();
            hs.put("login", "用户不存在");
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(hs);
        }
    };
    @PostMapping("/user")
    @RequiresRoles(logical = Logical.OR,value = {"管理员"})
    @ResponseBody
    @ApiOperation("验证用户登录是否有效")
    @MyLog(type = "接口访问", msg = "user接口访问")  //这里使用的自定义注解
    public String user(HttpServletRequest request) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("info","成功访问user接口！");
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    };


}
